import { createRouter, createWebHashHistory } from 'vue-router'
import Login from '@/views/Login.vue'
import Home from '@/views/Home.vue'
import SongTable from '@/components/SongTable.vue'
import PlayDetail from '@/components/PlayDetail.vue'
import MvDetail from '@/components/MvDetail.vue'
import User from '@/components/User.vue'
import Upload from '@/components/Upload.vue'
 
const routes = [
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/home',
    name: 'Home',
    component: Home,
    children:[
      {
        path:'',
        name: 'SongTable',
        component: SongTable,
      },
      {
        path:'/playdetail',
        name:'PlayDetail',
        component:PlayDetail
      },
      {
        path:'/upload',
        name:'Upload',
        component:Upload
      },
      {
        path:'/mvdetail',
        name:'MvDetail',
        component:MvDetail
      },
      {
        path:'/user',
        name:'User',
        component:User
      }
    ]
  }
  
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})
//路由首位
router.beforeEach((to,from,next)=>{
  if(to.path === '/login') return next()
  if(to.path === '/') return next('/login')
  const tokenstr = window.sessionStorage.getItem('token')
  if(!tokenstr) return next('/login')
  next()
})
export default router
